﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;

namespace XFSenseUI.XFItems
{
    public class SenseItemControl : SenseItemBase
    {
        public SenseControl Control { get { return _control; } set { _control = value; value.Owner = this; } }

        private SenseControl _control;
        private int _height;

        public SenseItemControl()
        {
            ItemType = XFItemType.Clickable;
            Style = new XFItemStyle
                    {
                        BorderBottomColor = Color.FromArgb(189, 182, 189),
                        DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                        TextColor = Color.Black,
                        TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedBackgroundImageString = "item-select-background",
                        SecondaryTextColor = Color.FromArgb(57, 52, 57),
                        SelectedTextColor = Color.White,
                        SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                        SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                        Padding = 11,
                        PaddingBottom = 12,
                        PaddingLeft = 10,
                        PaddingRight = 16
                    };

            Click += (o, e) => Control.OnClick();
            Focus += (o, e) => Control.OnFocus();
            Blur += (o, e) => Control.OnBlur();
            Refresh += (o, e) => Control.OnRefresh();
            Disposed += (o, e) => Control.Dispose();
        }

        public override XFItemClickResult GetClickResult()
        {
            return Control != null ? Control.GetClickResult() : base.GetClickResult();
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            int size = Control.GetHeight() + Style.PaddingTop + Style.PaddingBottom;
            _height = CalcRecs(Control.ItemRectangle.Width + Style.PaddingLeft);

            _height = (_height > size ? _height : size);
            return _height;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            if (Control != null)
            {
                int width = GetWidth();
                Rectangle rec = Control.ItemRectangle;
                if (rec.Width != 0)
                    width -= rec.Width + Style.PaddingRight;
                int aHeight = (GetHeight() / 2) - (rec.Height / 2);

                rec.X = width;
                rec.Y = aHeight;

                if (rec.Contains(x, y))
                    Control.OnMouseDown(x - width, y - aHeight);
            }
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            if (Control != null)
            {
                int width = GetWidth();
                Rectangle rec;
                if ((rec = Control.ItemRectangle).Width != 0)
                    width -= rec.Width + Style.PaddingRight;
                int aHeight = (GetHeight() / 2) - (rec.Height / 2);

                rec.X = width;
                rec.Y = aHeight;

                if (rec.Contains(x, y))
                    Control.OnMouseMove(x - width, y - aHeight);
            }
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            if (Control != null)
                Control.OnMouseUp();
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            var mRec = _mRec;
            mRec.Y += y;

            var sRec = _sRec;
            sRec.Y += y;

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);

            Rectangle rec = new Rectangle();
            int width = _mRec.X + _mRec.Width + Style.PaddingLeft;

            if (Control != null) rec = Control.ItemRectangle;

            if (!string.IsNullOrEmpty(MainText))
                g.DrawString(MainText, mRec, Style, fontMain, colorMain);
            if (!string.IsNullOrEmpty(SecondaryText))
                g.DrawString(SecondaryText, sRec, Style, fontSecondary, colorSecondary);

            if (rec.Width != 0 && Control != null)
            {
                int aHeight = (GetHeight() >> 1) - (rec.Height >> 1) + y;
                Control.ItemPaint(g, width, aHeight);
            }

            if (!string.IsNullOrEmpty(Icon))
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft + x, Style.PaddingTop + y, _iconSize, _iconSize));
        }
    }
}
